<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"yoursite.com","root":"/","scheme":"Pisces","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="面向对象综合实践">
<meta property="og:type" content="article">
<meta property="og:title" content="酒店前台客房管理系统2">
<meta property="og:url" content="http://yoursite.com/2020/04/20/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F2/index.html">
<meta property="og:site_name" content="很惭愧，就做了一点微小的工作">
<meta property="og:description" content="面向对象综合实践">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-04-20T10:07:48.000Z">
<meta property="article:modified_time" content="2020-05-17T15:43:42.251Z">
<meta property="article:author" content="文予">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="http://yoursite.com/2020/04/20/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F2/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>酒店前台客房管理系统2 | 很惭愧，就做了一点微小的工作</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">很惭愧，就做了一点微小的工作</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">念念不忘，必有回响</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2020/04/20/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/uploads/wenyu.png">
      <meta itemprop="name" content="文予">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="很惭愧，就做了一点微小的工作">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          酒店前台客房管理系统2
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-20 18:07:48" itemprop="dateCreated datePublished" datetime="2020-04-20T18:07:48+08:00">2020-04-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-05-17 23:43:42" itemprop="dateModified" datetime="2020-05-17T23:43:42+08:00">2020-05-17</time>
              </span>

          
            <div class="post-description">面向对象综合实践</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="Room类设计"><a href="#Room类设计" class="headerlink" title="Room类设计"></a><code>Room</code>类设计</h2><p><code>Room</code>中包含有入住客人的信息，因此将客人入住和退房的行为封装在<code>Room</code>中，供<code>Hotel</code><br>类的<code>checkin</code>和<code>checkout</code>两个业务调用。它们对当前客房状态进行判断，决定能否入住或者退房，并更新客房的客人信息。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">in</span><span class="params">(String customerName)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.customerName == <span class="keyword">null</span>) &#123;</span><br><span class="line">        <span class="keyword">this</span>.customerName = customerName;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">out</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">this</span>.customerName != <span class="keyword">null</span>) &#123;</span><br><span class="line">        customerName = <span class="keyword">null</span>;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Hotel类设计"><a href="#Hotel类设计" class="headerlink" title="Hotel类设计"></a><code>Hotel</code>类设计</h2><p><code>Hotel</code>的核心方法在于处理酒店的查询、入住和退房业务。</p>
<h3 id="查询所有客房"><a href="#查询所有客房" class="headerlink" title="查询所有客房"></a>查询所有客房</h3><p><code>search all</code>命令用二维表的形式显示打印酒店的所有客房编号及其状态，如果客房未入住则显示空白，否则显示入住客人名字。客房的<code>id</code>和客人信息均由<code>Room</code>类的方法获取。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">searchAll</span><span class="params">()</span> </span>&#123; <span class="comment">// 查询所有客房</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; HEIGHT - <span class="number">1</span>; i++) &#123;</span><br><span class="line">        <span class="comment">// 输出客房编号</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; WIDTH - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            System.out.print(rooms[i][j].getId() + <span class="string">"\t"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">        <span class="comment">// 输出客房状态</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; WIDTH - <span class="number">1</span>; j++) &#123;</span><br><span class="line">            System.out.print(rooms[i][j].getCustomerName() == <span class="keyword">null</span> ? <span class="string">"\t"</span> : rooms[i][j].getCustomerName() + <span class="string">"\t"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; <span class="number">8</span> * WIDTH; j++) &#123;</span><br><span class="line">            System.out.print(<span class="string">"-"</span>);</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println();</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="查询指定客房"><a href="#查询指定客房" class="headerlink" title="查询指定客房"></a>查询指定客房</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">searchByNo</span><span class="params">(String roomNo)</span> </span>&#123;<span class="comment">// 查询指定客房</span></span><br><span class="line">    <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">        <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">        <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            System.out.println(rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].getCustomerName() == <span class="keyword">null</span> ? <span class="string">"该客房没有客人"</span></span><br><span class="line">                    : roomNo + <span class="string">"\t"</span> + rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].getCustomerName());</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        System.out.println(<span class="string">"没有这个客房"</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="入住"><a href="#入住" class="headerlink" title="入住"></a>入住</h3><p>首先对客房编号进行检测，合法情况下调用<code>Room</code>类的<code>in()</code>方法，返回1表示成功入住；返回2表示该客房已有客人，入住失败；返回3表示客房编号参数错误，不存在该客房。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">checkIn</span><span class="params">(String roomNo, String name)</span> </span>&#123;<span class="comment">// 入住，先对客房编号进行检测</span></span><br><span class="line">        <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">            <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">            <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            <span class="keyword">if</span> (rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].in(name)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h3 id="退房"><a href="#退房" class="headerlink" title="退房"></a>退房</h3><p>与入住类似，返回1表示退房成功；返回2表示该客房没有客人，退房失败；返回3表示客房编号参数错误，不存在该客房。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">checkout</span><span class="params">(String roomNo)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">            <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">            <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            <span class="keyword">if</span> (rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].out()) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h2 id="客户端Client类实现"><a href="#客户端Client类实现" class="headerlink" title="客户端Client类实现"></a>客户端<code>Client</code>类实现</h2><p><code>Client</code>类用于实现酒店前台的工作流程，它在<code>main()</code>方法中创建<code>Hotel</code>对象，并根据控制台输入的指令调用相应的方法实现酒店前台管理。<br>看着他读入命令的第一个词决定调用哪个方法，根据不同的命令接收后续的多个参数。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        Hotel hotel = <span class="keyword">new</span> Hotel(<span class="string">"MiniHotel"</span>);</span><br><span class="line">        System.out.println(<span class="string">"欢迎入住"</span> + hotel.getHotelName() + <span class="string">"酒店"</span>);</span><br><span class="line">        Scanner sc = <span class="keyword">new</span> Scanner(System.in);</span><br><span class="line">        System.out.println(<span class="string">"请输入指令"</span>);</span><br><span class="line">        String command = sc.next();</span><br><span class="line">        String roomNo;</span><br><span class="line">        <span class="keyword">while</span> (!command.equalsIgnoreCase(<span class="string">"quit"</span>)) &#123;</span><br><span class="line">            <span class="keyword">if</span> (command.equalsIgnoreCase(<span class="string">"search"</span>)) &#123;</span><br><span class="line">                String para = sc.next();</span><br><span class="line">                <span class="keyword">if</span> (para.equals(<span class="string">"all"</span>)) &#123;</span><br><span class="line">                    hotel.searchAll();</span><br><span class="line">                &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                    hotel.searchByNo(para);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (command.equalsIgnoreCase(<span class="string">"in"</span>)) &#123;</span><br><span class="line">                roomNo = sc.next();<span class="comment">// 客房编号</span></span><br><span class="line">                String name = sc.next();<span class="comment">// 客户姓名</span></span><br><span class="line">                <span class="keyword">int</span> res = hotel.checkIn(roomNo, name);</span><br><span class="line">                <span class="keyword">if</span> (res == <span class="number">1</span>) &#123;</span><br><span class="line">                    System.out.println(name + <span class="string">"成功入住"</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res == <span class="number">2</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"该房间已有客人"</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res == <span class="number">3</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"房间号输入错误"</span>);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125; <span class="keyword">else</span> <span class="keyword">if</span> (command.equalsIgnoreCase(<span class="string">"out"</span>)) &#123;</span><br><span class="line">                roomNo = sc.next();<span class="comment">// 客房编号</span></span><br><span class="line">                <span class="keyword">int</span> res = hotel.checkout(roomNo);<span class="comment">// 调用退房方法</span></span><br><span class="line">                <span class="keyword">if</span> (res == <span class="number">1</span>) &#123;</span><br><span class="line">                    System.out.println(roomNo + <span class="string">"成功退房"</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res == <span class="number">2</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"该房间没有客人"</span>);</span><br><span class="line">                &#125; <span class="keyword">else</span> <span class="keyword">if</span> (res == <span class="number">3</span>) &#123;</span><br><span class="line">                    System.out.println(<span class="string">"房间号输入错误"</span>);</span><br><span class="line">                &#125;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.println(<span class="string">"没有该指令"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println(<span class="string">"请输入您的指令"</span>);</span><br><span class="line">            command = sc.next();</span><br><span class="line">        &#125; <span class="comment">// while</span></span><br><span class="line">        System.out.println(<span class="string">"欢迎下次光临"</span> + hotel.getHotelName());</span><br><span class="line">        System.exit(<span class="number">0</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="另附"><a href="#另附" class="headerlink" title="另附"></a>另附</h2><p><code>Room</code>类、<code>Hotel</code>类完整代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Room</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> String id;</span><br><span class="line">    <span class="keyword">private</span> String customerName;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Room</span><span class="params">(String id, String customerName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>();</span><br><span class="line">        <span class="keyword">this</span>.id = id;</span><br><span class="line">        <span class="keyword">this</span>.customerName = customerName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Room</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> floor, <span class="keyword">int</span> number)</span> </span>&#123;</span><br><span class="line">        id = (floor &lt;= <span class="number">9</span> ? <span class="string">"0"</span> + floor : <span class="string">""</span> + floor) + (number &lt;= <span class="number">9</span> ? <span class="string">"0"</span> + number : <span class="string">""</span> + number);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getId</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">in</span><span class="params">(String customerName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.customerName == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">this</span>.customerName = customerName;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">out</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (<span class="keyword">this</span>.customerName != <span class="keyword">null</span>) &#123;</span><br><span class="line">            customerName = <span class="keyword">null</span>;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getCustomerName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> customerName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setCustomerName</span><span class="params">(String customerName)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.customerName = customerName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Hotel</span> </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> HEIGHT = <span class="number">6</span>;</span><br><span class="line">    <span class="keyword">int</span> WIDTH = <span class="number">12</span>;</span><br><span class="line">    <span class="keyword">private</span> String HotelName;</span><br><span class="line">    Room[][] rooms = <span class="keyword">new</span> Room[HEIGHT][WIDTH];</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">Hotel</span><span class="params">(String hotelName)</span> </span>&#123;</span><br><span class="line">        HotelName = hotelName;</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; HEIGHT - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; WIDTH - <span class="number">1</span>; j++) &#123;</span><br><span class="line">                <span class="comment">// 创建并初始化每一个Room对象</span></span><br><span class="line">                rooms[i][j] = <span class="keyword">new</span> Room();</span><br><span class="line">                rooms[i][j].setId(i + <span class="number">1</span>, j + <span class="number">1</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">private</span> <span class="keyword">boolean</span> <span class="title">testRoomNo</span><span class="params">(String roomNo)</span> </span>&#123; <span class="comment">// 客房编号合法性判断</span></span><br><span class="line">        <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">        <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">        <span class="keyword">if</span> (height &lt; <span class="number">1</span> || height &gt; HEIGHT || width &lt; <span class="number">1</span> || width &gt; WIDTH) &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">false</span>;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="keyword">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">searchAll</span><span class="params">()</span> </span>&#123; <span class="comment">// 查询所有客房</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i = <span class="number">0</span>; i &lt; HEIGHT - <span class="number">1</span>; i++) &#123;</span><br><span class="line">            <span class="comment">// 输出客房编号</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; WIDTH - <span class="number">1</span>; j++) &#123;</span><br><span class="line">                System.out.print(rooms[i][j].getId() + <span class="string">"\t"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">            <span class="comment">// 输出客房状态</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; WIDTH - <span class="number">1</span>; j++) &#123;</span><br><span class="line">                System.out.print(rooms[i][j].getCustomerName() == <span class="keyword">null</span> ? <span class="string">"\t"</span> : rooms[i][j].getCustomerName() + <span class="string">"\t"</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">            <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">1</span>; j &lt; <span class="number">8</span> * WIDTH; j++) &#123;</span><br><span class="line">                System.out.print(<span class="string">"-"</span>);</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">searchByNo</span><span class="params">(String roomNo)</span> </span>&#123;<span class="comment">// 查询指定客房</span></span><br><span class="line">        <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">            <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">            <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            System.out.println(rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].getCustomerName() == <span class="keyword">null</span> ? <span class="string">"该客房没有客人"</span></span><br><span class="line">                    : roomNo + <span class="string">"\t"</span> + rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].getCustomerName());</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            System.out.println(<span class="string">"没有这个客房"</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">checkIn</span><span class="params">(String roomNo, String name)</span> </span>&#123;<span class="comment">// 入住，先对客房编号进行检测</span></span><br><span class="line">        <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">            <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">            <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            <span class="keyword">if</span> (rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].in(name)) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">checkout</span><span class="params">(String roomNo)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (testRoomNo(roomNo)) &#123;</span><br><span class="line">            <span class="keyword">int</span> height = Integer.parseInt(roomNo.substring(<span class="number">0</span>, <span class="number">2</span>));</span><br><span class="line">            <span class="keyword">int</span> width = Integer.parseInt(roomNo.substring(<span class="number">2</span>, <span class="number">4</span>));</span><br><span class="line">            <span class="keyword">if</span> (rooms[height - <span class="number">1</span>][width - <span class="number">1</span>].out()) &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">                <span class="keyword">return</span> <span class="number">2</span>;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="number">3</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getHotelName</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> HotelName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2020/04/17/%E9%85%92%E5%BA%97%E5%89%8D%E5%8F%B0%E5%AE%A2%E6%88%BF%E7%AE%A1%E7%90%86%E7%B3%BB%E7%BB%9F1/" rel="prev" title="酒店前台客房管理系统1">
      <i class="fa fa-chevron-left"></i> 酒店前台客房管理系统1
    </a></div>
      <div class="post-nav-item">
    <a href="/2020/04/20/Java%E6%97%A5%E6%8B%B1%E4%B8%80%E5%8D%92-day8/" rel="next" title="Java日拱一卒-day8">
      Java日拱一卒-day8 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Room类设计"><span class="nav-number">1.</span> <span class="nav-text">Room类设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Hotel类设计"><span class="nav-number">2.</span> <span class="nav-text">Hotel类设计</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#查询所有客房"><span class="nav-number">2.1.</span> <span class="nav-text">查询所有客房</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#查询指定客房"><span class="nav-number">2.2.</span> <span class="nav-text">查询指定客房</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#入住"><span class="nav-number">2.3.</span> <span class="nav-text">入住</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#退房"><span class="nav-number">2.4.</span> <span class="nav-text">退房</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#客户端Client类实现"><span class="nav-number">3.</span> <span class="nav-text">客户端Client类实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#另附"><span class="nav-number">4.</span> <span class="nav-text">另附</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="文予"
      src="/uploads/wenyu.png">
  <p class="site-author-name" itemprop="name">文予</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">29</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文予</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://pisces.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Pisces</a> 强力驱动
  </div>


        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>











<script>
if (document.querySelectorAll('pre.mermaid').length) {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/mermaid@8/dist/mermaid.min.js', () => {
    mermaid.initialize({
      theme    : 'forest',
      logLevel : 3,
      flowchart: { curve     : 'linear' },
      gantt    : { axisFormat: '%m/%d/%Y' },
      sequence : { actorMargin: 50 }
    });
  }, window.mermaid);
}
</script>


  

  

</body>
</html>
